

var swakstats = {
	incrementOutwitOptIn: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OW_OPTINCOUNT);
			count++;
			ssopts.setIntPref(SMACK_STAT_OW_OPTINCOUNT,count);
		}
		catch(x){}
	},
	incrementOutwitSelected: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OW_SELECTED);
			count++;
			ssopts.setIntPref(SMACK_STAT_OW_SELECTED,count);
		}
		catch(x){}
	},	
	incrementOutwitVote: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OW_VOTECOUNT);
			count++;
			ssopts.setIntPref(SMACK_STAT_OW_VOTECOUNT,count);
		}
		catch(x){}		
	},
	incrementOutwitPoint: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OW_POINTCOUNT);
			count++;
			ssopts.setIntPref(SMACK_STAT_OW_POINTCOUNT,count);
		}
		catch(x){}		
	},
	incrementSmackPotPlayed: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_SP_PLAYED);
			count++;
			ssopts.setIntPref(SMACK_STAT_SP_PLAYED,count);
		}
		catch(x){}		
	},
	incrementSmackPotBet: function(coins) {
		if (coins == null) coins = 0;
		try {
			var count = SS_getPreference(SMACK_STAT_SP_COINBETCOUNT);
			count = count+coins;
			ssopts.setIntPref(SMACK_STAT_SP_COINBETCOUNT,count);
		}
		catch(x){}		
	},
	incrementSmackPotCoinsWon: function(coins) {
		if (coins == null) coins = 0;
		try {
			var count = SS_getPreference(SMACK_STAT_SP_COINWONCOUNT);
			count = count+coins;
			ssopts.setIntPref(SMACK_STAT_SP_COINWONCOUNT,count);
		}
		catch(x){}		
	},
	incrementGuruGuessPlayed: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_GG_PLAYED);
			count++;
			ssopts.setIntPref(SMACK_STAT_GG_PLAYED,count);
		}
		catch(x){}		
	}		
}

function swakBuildOTSResult() {
	var otsresult = null;
	try {
		otsresult = new SwakOTSResult();
		
		var results = swaksmack.SS_JF.getOTSResultsPercents();
		var ans = swaksmack.SS_JF.getOTSResults();
		//var nv = swaksmack.SS_JF.calcNumOTSPlayers(results);
		
		otsresult.numVoters = swaksmack.SS_JF.calcNumOTSPlayers(results);
		otsresult.answers = ans.slice();
		otsresult.contestant = swaksmack.SS_JF.getContestant();
		otsresult.oid = swaksmack.SS_JF.getLOID();
		otsresult.date = new Date();
		otsresult.amountWon = swaksmack.SS_JF.getAmountWon();
		if (otsresult.amountWon > 0) {
			otsresult.winner = true;
		}
		otsresult.contestantAvatar = swaksmack.SS_JF.getContestantAvatar();
		otsresult.question = swaksmack.SS_JF.getOTSQuestion();
		otsresult.winningAnswers =swaksmack.SS_JF.getOTSWinningAnswers();
	}
	catch (x) {
		swakAddException(x);
		otsresult = null;
	}
	
	return otsresult;
}

function SwakOTSResult() {
	//use liveobject id as oid
	this.oid = null;
	this.date = null;
	this.contestant = null;
	this.contestantAvatar = null;
	this.winner = false;
	this.amountWon = 0;
	this.question = null;
	this.winningAnswerStr = null;
	this.winningAnswerNum = -1;
	this.numVoters = 0;
	//array of answers
	this.answers = null;
	this.winningAnswers = null;
	this.innerHTML = null;
	
	this.toString = function() {
		var str = 'Date: ' + this.date;
		str+='\n';
		str+='OID: ' +this.oid;
		str+='\n';
		str+='Question: ' +this.question;
		str+='\n';
		str+='Answer text: ' +this.winningAnswerStr;
		str+='\n';
		str+='Answer number: ' +this.winningAnswerNum;
		str+='\n';
		str+='Contestant: ' +this.contestant;
		str+='\n';
		str+='Contestant avatar: ' +this.contestantAvatar;
		str+='\n';
		str+='Winner: ' +this.winner;
		str+='\n';
		str+='Amount won: ' +this.amountWon;
		str+='\n';
		str+='Number of voters: ' +this.numVoters;
		str+='\n';
		str+='Answers: ' +this.answers;
		str+='\n';
		str+='Winning Answers: ' +this.winningAnswers;
		return str;
	}
}

function swakFindOTSWinPct(results) {
	if (results == null) return -1;
	var windex = -1;
	var low = 100.0;
	try {
		for (var i=0;i<results.length;i++){
			if (results[i] < low) {
				low = results[i];
				windex = i;
			}
		}
	}
	catch (x) {
		swakSetException(x);
		windex = -1;
	}
	return windex;
}

function swakGetOTSWinIndex_ORG(results,winners,rows) {
	if (true) return;
	if (results == null || winners == null) return null;
	var win = null;
	var windex = new Array();
	for (var i=0;i<winners.length;i++) {
		win = winners[i];
		try{
		for (var j=0;j<results.length;j++) {
			if (results[j].id == win) {
				windex.push(j);
				if (rows != null) {
					rows[j].className = 'swakOTSRowWinner';
				}
			}
			else {
				if (rows != null & rows[j].className != 'swakOTSRowWinner') {
					var cells = rows[j].getElementsByTagName("td");
					//tag cells as loser, don't tag first td as that is the avatar spot
					for (var cc=1;cc<cells.length;cc++) {
						cells[cc].className = 'swakOTSTDLoser';
					}
					//fyi: use this tag the entire row
					//rows[j].className = 'swakOTSRowLoser';
				}
			}
		}
	}catch(x) {alert(x);}
	}
	return windex;
}



function swakGetOTSWinIndex(results,winners,rows) {
	if (results == null || winners == null || rows == null) return null;
	var windex = new Array();
	var rlen = results.length;
	//magic numbers suck, but there should not be more than 6 ots ans, but I've heard of 7
	if (rlen >10) {
		rlen = 10//for now limit to 10;
	}
	try {
		if (winners.length == 1) {
			win = winners[0];
			for (var jj=0;jj<rlen;jj++) {
				if (results[jj].id == winners[0]) {
					windex.push(jj);
					if (rows != null && rows[jj] != null) {
						var wrow = rows[jj];
						wrow.className = 'swakOTSRowWinner';
					}
				}
				else {
					if (rows != null && rows[jj] != null) {
						var lrow = rows[jj];
						if (lrow.className != 'swakOTSRowWinner') {
							var cells = lrow.getElementsByTagName("td");
							if (cells == null) continue;
							//tag cells as loser, don't tag first td as that is the avatar spot
							for (var cc=1;cc<cells.length;cc++) {
								if (cells[cc] != null) {
									cells[cc].className = 'swakOTSTDLoser';
								}
							}
							//fyi: use this tag the entire row
							//rows[j].className = 'swakOTSRowLoser';
						}
					}
				}
			} //end for jj
		}
		else {
			var winids = winners.join();
			for (var k=0;k<rlen;k++) {
				var wre = new RegExp(results[k].id,"ig");
				var wf = wre.test(winids);
				if (wf == true) {
					windex.push(k);
					if (rows != null && rows[k] != null) {
						var wrow = rows[k];
						wrow.className = 'swakOTSRowWinner';
					}
				}
				else {
					if (rows != null && rows[k] != null) {
						var lrow = rows[k];
						if (lrow.className != 'swakOTSRowWinner') {
							var cells = lrow.getElementsByTagName("td");
							if (cells == null) continue;
							//safety abort
							if (cells.length >30) continue;
							//tag cells as loser, don't tag first td as that is the avatar spot
							for (var cc=1;cc<cells.length;cc++) {
								cells[cc].className = 'swakOTSTDLoser';
							}
							//fyi: use this tag the entire row
							//rows[j].className = 'swakOTSRowLoser';
						}
					}
				}
			} //end for k
		}
	}
	catch(x) {
		swakAddException(x);
		//alert(x);
		windex = null;
	}

	return windex;
}